package com.sk.bilibili.other;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class SessionInterceptor implements Interceptor {

    private static final String B_SESSION_ID="bsessionid";
    private String bSessionId="";

    public SessionInterceptor(String bSessionId) {
        if(bSessionId!=null)this.bSessionId=bSessionId;
    }

    @NonNull
    @Override//使用拦截器保留Cookie
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request().newBuilder().addHeader(B_SESSION_ID,bSessionId).build();
        Response response = chain.proceed(request);
        String header = response.header(B_SESSION_ID);
        if(header!=null)bSessionId = header;//不为null才更新
        return response;
    }

}